import os.path
import random
import string

class Creature():

    def __init__(self, id, sandbox, parentid):
        self.parentid = parentid
        self.sandbox = sandbox
        self.id = id
        self.old_dns = "creature_" + str(self.parentid) + ".py"
        self.new_dns = "creature_" + str(self.id) + ".py"

    def live(self):
        self.mutate()

    def mutate(self):
        with open(self.old_dns, "r") as file:
            dns = file.read()
        for i in range(0, 5):
            index = random.randint(0, len(dns))
            amount = random.randint(0, 3)
            for j in range(amount):
                first_segment = dns[:index + j]
                second_segment = dns[index + j:]
                new_char = string.printable[random.randint(0, 99)]
                tmp_dns = first_segment + new_char + second_segment
                dns = tmp_dns
        with open(self.new_dns, "w") as file:
            file.writelines(tmp_dns)
        self.sandbox.create_creature(self.new_dns, self.id)
    